package com.metaapp.demo.net
import com.metaapp.demo.MetaApplication
import com.metaapp.demo.R
import com.metaapp.demo.bean.BaseBean
import com.modong.jianjian.base.BasePresenter
import com.modong.jianjian.base.IBaseView
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * Created by Clei on 2019/3/14.
 * Description :
 */
abstract class ApiObserver<T : BaseBean> : Observer<T> {

    companion object {
        const val SHOW_NONE = 0//不现实任何加载状态视图
        const val SHOW_ONLY_LOADING = 1//只显示loading视图
        const val SHOW_ONLY_EMPTY = 2//只显示空视图
        const val SHOW_ALL = 3//显示所有加载状态视图
    }

    private var mView: IBaseView? = null
    private var mPresenter: BasePresenter<*>? = null
    private var isShowLoadingView = false//接口是否有加载中视图
    private var isShowLoadFailedView = false//接口是否有加载失败页面

    constructor(presenter: BasePresenter<*>?) {
        this.mView = presenter?.mView
        this.mPresenter = presenter
        setShowLoadStatusViewPolicy(SHOW_ONLY_LOADING)
    }

    /**
     * @param statusViewPolicy [.SHOW_NONE],[.SHOW_ONLY_LOADING],[.SHOW_ONLY_EMPTY],[.SHOW_ALL]
     */
    constructor(presenter: BasePresenter<*>?, statusViewPolicy: Int) : this(presenter) {
        setShowLoadStatusViewPolicy(statusViewPolicy)
    }

    /**
     * 设置视图显示策略
     */
    private fun setShowLoadStatusViewPolicy(showLoadStatusViewPolicy: Int) {
        when (showLoadStatusViewPolicy) {
            SHOW_NONE -> {
                isShowLoadingView = false
                isShowLoadFailedView = false
            }
            SHOW_ONLY_LOADING -> {
                isShowLoadingView = true
                isShowLoadFailedView = false
            }
            SHOW_ONLY_EMPTY -> {
                isShowLoadingView = false
                isShowLoadFailedView = true
            }
            SHOW_ALL -> {
                isShowLoadingView = true
                isShowLoadFailedView = true
            }
            else -> {
            }
        }
    }


    override fun onSubscribe(d: Disposable) {
        if (isShowLoadingView) {
            mView?.setLoadingView(true)
        }
        mPresenter?.addSubscribe(d)
    }

    override fun onNext(t: T) {

        if (isShowLoadingView) {
            mView?.setLoadingView(false)
            if (isShowLoadFailedView) {
                mView?.setLoadFailedView(false)
            }
        }

        when (t.code) {
            200 -> doOnSuccess(t, t.message)
            else -> handleFailureResult(t.code, t.message)
        }
    }

    private fun handleFailureResult(errorCode: Int, errorMsg: String?) {
        when (errorCode) {
            -100 -> mView?.onUserTokenError()
            else -> {
                doOnFailure(errorCode, errorMsg)
            }
        }
    }


    /**
     * 处理对应业务状态码
     */
    protected open fun doOnFailure(errorCode: Int, errorMsg: String?) {
        if (isShowLoadFailedView) {
            mView?.setLoadFailedView(true)
        } else {
            mView?.shortToast(errorMsg ?: MetaApplication.context.getString(R.string.tips_connected_error))
        } 
    }

    protected abstract fun doOnSuccess(data: T, msg: String?)

    override fun onComplete() {

    }

    override fun onError(e: Throwable) {
        e.printStackTrace()
        if (isShowLoadingView) {
            mView?.setLoadingView(false)
        }
        if (isShowLoadFailedView) {
            mView?.setLoadFailedView(true)
        } else {
            mView?.shortToast(MetaApplication.context.getString(R.string.tips_connected_error))
        }
    }
}